﻿//模态框操作js函数


/*******弹出表单*********/
function ShowModal(actionUrl, param, title) {
    var $modal = $("#modal-form");
    //表单初始化
    $(".modal-title", $modal).html(title);
    $("#modal-content", $modal).attr("action", actionUrl);

    $.ajax({
        type: "GET",
        url: actionUrl,
        data: param,
        beforeSend: function () {
            //
        },
        success: function (result) {
            $("#modal-content").html(result);
            $('#modal-form').modal('show');
            RegisterForm();//通过Ajax加载返回的页面原有MVC属性验证将失效，需要重新注册验证脚本。
        },
        error: function () {
            //
        },
        complete: function () {
            //
        }
    });
}

/*******注册验证脚本，通过Ajax返回的页面原有MVC属性验证将失效，需要重新注册验证脚本*********/
function RegisterForm() {
    $('#modal-content').removeData('validator');
    $('#modal-content').removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse('#modal-content');
}

/*******保存表单*********/
function SaveModal(table) {

    var actionUrl = $("#modal-content").attr("action");
    var $form = $("#modal-content");
    if (!$form.valid()) {
        return;
    }
    $.ajax({
        type: "POST",
        url: actionUrl,
        data: $form.serialize(),
        success: function (result) {
            if (result.IsSuccess) {
                toastr.success(result.Message);
                $('#modal-form').modal('hide');
                $(table).bootstrapTable('refresh');
            }
            else {
                toastr.error(result.Message);
            }
        },
        error: function () {
            toastr.error('网络错误，请重新提交！');
        }
    });
}

//表格单行删除
function MultDelete(table, param, actionUrl) {
    WinMsg.confirm({ message: "确认要删除选择的数据吗？" }).on(function (e) {
        if (!e) {
            return;
        }
        $.ajax({
            type: "post",
            url: actionUrl,
            data: param,
            success: function (result) {
                if (result.IsSuccess) {
                    toastr.success(result.Message);
                    $(table).bootstrapTable('refresh');
                }
                else {
                    toastr.error(result.Message);
                }
            },
            error: function () {
                toastr.error('网络错误，请重新提交！');
            }
        });
    });
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//带提示选择的ajax提交
jQuery.confirmAjax = function (msg, ajaxOptions) {
    /// <summary>
    /// 带提示选择的ajax提交
    /// </summary>
    /// <param name="msg">提示信息</param>
    /// <param name="ajaxOptions">$.ajax的配置项，新增table节点用来实现成功后表格的刷新，默认POST提交，返回数据类型为json，根据返回结果的IsSuccess判断成功，操作失败输出结果的Message信息</param>
    WinMsg.confirm({
        title:"操作提示",
        message: msg,
        btnok: "确定",
        btncl: "取消"
    }).on(function (e) {
        if (!e) {
            return;
        }
        loading.start();
        var opt = {
            type: "POST",
            dataType: "json",
            success: function (result) {
                if (result.IsSuccess) {
                    if (result.Message) {
                        toastr.success(result.Message);
                    }
                    if (ajaxOptions.search && typeof ajaxOptions.search == 'function') {
                        ajaxOptions.search();
                    }
                }
                else {
                    toastr.error(result.Message);
                }
            },
            error: function (result, textStatus) {
                toastr.error('Operation failed, please try again!');
            },
            complete: function () {
                loading.end();
            }
        };
        $.extend(opt, ajaxOptions);
        $.ajax(opt);
    });
}
//模态窗口展示
jQuery.modalBox = function (title, ajaxOptions) {
    /// <summary>
    /// 模态窗口展示
    /// </summary>
    /// <param name="title">窗口标题</param>
    /// <param name="ajaxOptions">$.ajax的配置项，默认GET提交，成功后显示窗口，根据ajax的url自动配置表单<br/>
    /// 新增modaltype节点用来初始化窗体按钮<br/>
    /// 新增search函数用来绑定表单刷新等更新方法<br/>
    /// 新增unsave节点用来控制表单不默认提交</param>
    loading.start();
    $("#modal-form .modal-dialog").removeClass("modal-lg");
    var $modal = $("#modal-form");
    //表单初始化
    $(".modal-title", $modal).html(title);
    $("#modal-content", $modal).attr("action", ajaxOptions.url);
    $("#modal-content").unbind("submit");
    $("#btnSave").unbind("click");
    if (!ajaxOptions.modaltype) {
        ajaxOptions.modaltype = "save";
    }
    switch (ajaxOptions.modaltype.toLowerCase()) {
        case "onlyclose":
            ajaxOptions.unsave = true;
            $("#btnSave").hide();
            break;
        case "save":
        default:
            $("#btnSave").show();
            break;
    }
    var opt = {
        type: "GET",
        success: function (result) {
            $("#modal-content").html(result);
            $('#modal-form').modal('show');
            //通过Ajax加载返回的页面原有MVC属性验证将失效，需要重新注册验证脚本。
            $('#modal-content').removeData('validator');
            $('#modal-content').removeData('unobtrusiveValidation');
            $.validator.unobtrusive.parse('#modal-content');
            if (!ajaxOptions.unsave) {
                $("#btnSave").click(function () {
                    $("#modal-content").formSave({ search: ajaxOptions.search });
                });
                $("#modal-content").submit(function () {
                    $("#modal-content").formSave({ search: ajaxOptions.search });
                    return false;
                });
            }
            $("#modal-content").append('<input type="submit" style="display:none;" />');

        },
        complete: function () {
            loading.end();
        }
    }
    $.extend(opt, ajaxOptions);
    $.ajax(opt);
}
//验证控件样式设置
$(function () {
    toastr.options = { positionClass: "toast-top-center", showDuration: "150", hideDuration: "2000", showMethod: "fadeIn" };
    $.validator.setDefaults({
        highlight: function (element) {
            $(element).closest('.form-group').addClass('has-error');
        }
    });
    $.validator.unobtrusive.options = {
        errorClass: 'text-danger',
        success: function (label) {
            label.closest('.form-group').removeClass('has-error');
            label.remove();
        },
        errorPlacement: function (error, element) {
            element.parent('div').append(error);
        }
    };
});
//form表单提交保存
(function ($) {
    $.fn.formSave = function (ajaxOptions) {
        /// <summary>
        /// form表单提交保存
        /// </summary>
        /// <param name="ajaxOptions">$.ajax的配置项，默认POST提交，返回数据类型为json，根据返回结果的IsSuccess判断成功，操作失败输出结果的Message信息，新增search函数用来绑定表单刷新等更新方法</param>
        loading.start();
        var actionUrl = $(this).attr("action");
        var $form = $(this);
        if (!$form.valid()) {
            loading.end();
            return;
        }
        var data = {};
        $form.serializeArray().map(function (x) { data[x.name] = x.value; });
        $.extend(data, ajaxOptions.data);
        var opt = {
            type: "POST",
            url: actionUrl,
            data: data,
            success: function (result) {
                if (result.IsSuccess) {
                    if (result.Message) {
                        toastr.success(result.Message);
                    }
                    $('#modal-form').modal('hide');
                    if (ajaxOptions.search && typeof ajaxOptions.search == 'function') {
                        ajaxOptions.search();
                    }
                }
                else {
                    toastr.error(result.Message);
                }
            },
            error: function () {
                toastr.error('Operation failed, please try again!');
            },
            complete: function () {
                loading.end();
            }
        }
        $.extend(opt, ajaxOptions);
        $.ajax(opt);
    }
})(jQuery);